package cn.edu.sgu.www.mhxysy.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.ServletContext;

/**
 * springmvc配置类
 * @author heyunlin
 * @version 1.0
 */
@Configuration
@ServletComponentScan("cn.edu.sgu.www.mhxysy.filter")
public class SpringMvcConfig implements WebMvcConfigurer {

    @Value("${uploads.path}")
    private String uploadPath;

    /**
     * 添加静态资源路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源路径
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:static/")
                .addResourceLocations("file:" + uploadPath + "/");
        // 解决knife4j访问失败问题
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 设置SESSION_ID
     * @return ServletContextInitializer
     */
    @Bean
    public ServletContextInitializer servletContextInitializer() {
        return new ServletContextInitializer() {
            @Override
            public void onStartup(ServletContext servletContext) {
                servletContext.getSessionCookieConfig().setName("MHXYSY_JSESSIONID");
            }
        };
    }

}